from DGA.code.enviserv.dga import enviserv_dga
from DGA.code.padcrypt.dga import padcrypt_dga
from DGA.gen_random_date import *
from DGA.code.bigviktor.dga import *
from DGA.code.ccleaner.dga import *
import pandas as pd
import random


server_name = []
domains = []
date = []
root_path = "/Users/ryantong/0bjtu/complex_network"
all_size = 10000
b_lengh = 19


def get_servers_list():
    all_servers = []
    for i in range(all_size):
        all_servers.append("server_" + str(i))
    return all_servers


def get_servers_random(rm, n):
    x = get_servers_list()
    if rm is not None:
        for rm_1 in rm:
            if rm_1 in x:
                x.remove(rm_1)
    return random.sample(x, n)


def gen_one_data(a, b_1, b_lengh, c, csv_name, lenth):
    b = []
    a_all = []
    b_all = []
    c_all = []
    for k in range(b_lengh):
        random.shuffle(b)
        b += b_1

    for i in range(5):
        c = randomDateList(start, end, lenth)
        a_all += a
        b_all += b
        c_all += c
        random.shuffle(a)
        random.shuffle(b)
        random.shuffle(c)

    # 字典中的key值即为csv中列名
    dataframe = pd.DataFrame({'server_name': a_all, 'domains': b_all, 'date': c_all})
    # 将DataFrame存储为csv,index表示是否显示行名，default=True
    dataframe.to_csv(root_path + "/"+csv_name+".csv", index=False, sep=',')
    return a_all, b_all, c_all


def read_csv(path, lengh):
    whites = []
    df = pd.read_csv(path, usecols=[1], nrows=lengh)
    for i in range(lengh):
        whites.append(df["url"][i])
    return whites


def gen_white_names(lengh):
    all_whites = read_csv(root_path + "/top-1m.csv", lengh)
    random.shuffle(all_whites)
    return all_whites


start = "2020-07-11 18:56:23"
end = "2020-07-31 18:56:23"
rm_list = []
for m in range(2):
    # bigviktor
    lenth_bigviktor = 931
    lenth_bigviktor_dg = 49

    dg = domain_generator(2020)
    a_bigviktor = []
    b_bigviktor = []
    c_bigviktor = []

    a_bigviktor_1 = get_servers_random(None, lenth_bigviktor)
    b_bigviktor_1_1 = dg.bigviktor_generate_domains(int(lenth_bigviktor_dg))
    b_bigviktor_1 = []
    c_bigviktor_1 = randomDateList(start, end, lenth_bigviktor)

    bigviktor_a, bigviktor_b, bigviktor_c = gen_one_data(a_bigviktor_1, b_bigviktor_1_1, b_lengh, c_bigviktor_1, "bigviktor_"+str(m), lenth_bigviktor)
    server_name += bigviktor_a
    domains += bigviktor_b
    date += bigviktor_c

    # ccleaner
    lenth_ccleaner = 532
    a_ccleaner = []
    b_ccleaner = []
    c_ccleaner = []
    rm_list = rm_list + a_bigviktor_1
    a_ccleaner_1_1 = get_servers_random(rm_list, lenth_ccleaner-2)
    a_ccleaner_1_2 = random.sample(rm_list, 2)  # bigviktor 中同时有两个被感染了bigviktor
    a_ccleaner_1 = a_ccleaner_1_1 + a_ccleaner_1_2
    b_ccleaner_1_1 = ccleaner_dga(1970, 7, 28, ['com'])
    b_ccleaner_1 = []
    c_ccleaner_1 = randomDateList(start, end, lenth_ccleaner)
    ccleaner_a, ccleaner_b, ccleaner_c = gen_one_data(a_ccleaner_1, b_ccleaner_1_1, b_lengh, c_ccleaner_1, "ccleaner_"+str(m), lenth_ccleaner)
    server_name += ccleaner_a
    domains += ccleaner_b
    date += ccleaner_c


    # enviserv
    lenth_enviserv = 912
    a_enviserv = []
    b_enviserv = []
    c_enviserv = []
    rm_list = rm_list + a_ccleaner_1
    a_enviserv_1_1 = get_servers_random(rm_list, lenth_enviserv-5)
    a_enviserv_1_2 = random.sample(rm_list, 5)  # bigviktor 中同时有两个被感染了bigviktor
    a_enviserv_1 = a_enviserv_1_1 + a_enviserv_1_2
    b_enviserv_1_1 = enviserv_dga("ryantong", 48, ['com', 'net', 'org', 'info','biz', 'in'])
    b_enviserv_1 = []
    c_enviserv_1 = randomDateList(start, end, lenth_enviserv)
    enviserv_a, enviserv_b, enviserv_c = gen_one_data(a_enviserv_1, b_enviserv_1_1, b_lengh, c_enviserv_1, "enviserv_"+str(m), lenth_enviserv)
    server_name += enviserv_a
    domains += enviserv_b
    date += enviserv_c

    # padcrypt
    lenth_padcrypt = 1368
    a_padcrypt = []
    b_padcrypt = []
    c_padcrypt = []
    rm_list = rm_list + a_enviserv_1
    a_padcrypt_1_1 = get_servers_random(rm_list, lenth_padcrypt-3)
    a_padcrypt_1_2 = random.sample(rm_list, 3)  # bigviktor 中同时有两个被感染了bigviktor
    a_padcrypt_1 = a_padcrypt_1_1 + a_padcrypt_1_2
    b_padcrypt_1_1_1 = padcrypt_dga(datetime.now(), "11.37.0.0")
    b_padcrypt_1_1 = []
    for domain in b_padcrypt_1_1_1:
        b_padcrypt_1_1.append(domain)
    b_padcrypt_1 = []
    c_padcrypt_1 = randomDateList(start, end, lenth_padcrypt)
    padcrypt_a, padcrypt_b, padcrypt_c = gen_one_data(a_padcrypt_1, b_padcrypt_1_1, b_lengh, c_padcrypt_1, "padcrypt_"+str(m), lenth_padcrypt)
    server_name += padcrypt_a
    domains += padcrypt_b
    date += padcrypt_c

    # white names
    a_whites = []
    b_whites = []
    c_whites = []
    a_whites_1 = get_servers_list()
    b_whites_1 = gen_white_names(1000)
    c_whites_1 = randomDateList(start, end, all_size)
    whites_a, whites_b, whites_c = gen_one_data(a_whites_1, b_whites_1, 10, c_whites_1, "whites_"+str(m), all_size)
    server_name += whites_a
    domains += whites_b
    date += whites_c
    start = "2020-08-01 18:56:23"
    end = "2020-08-31 18:56:23"

random.shuffle(server_name)
random.shuffle(domains)
random.shuffle(date)
# 字典中的key值即为csv中列名
dataframe = pd.DataFrame({'server_name': server_name, 'domains': domains, 'data': date})

# 将DataFrame存储为csv,index表示是否显示行名，default=True
dataframe.to_csv(root_path + "/datas.csv", index=False, sep=',')

